package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.object.pojo.AppointMent;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AppointMentService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author jing
 * @date 2020/9/2117:36
 * @Description:预约
 */
@RestController
@RequestMapping("/appointMent")
public class AppointMentController {

    @Autowired
    private AppointMentService appointMentService;
    @Autowired
    private OperaPswService operaPswService;


    /**
     * @param phone     用户手机号
     * @param areaId    区域id
     * @param machineNo
     * @param userCode
     * @param pageNo
     * @param rowCount
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 分页查询预约中的车辆
     * @createDate 2020/9/21 17:43
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String phone, Integer areaId, String machineNo, String userCode,
                                @RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNo) {
        List<AppointMent> appointMents = appointMentService.queryPage(phone, areaId, machineNo, userCode, pageNo, rowCount);
        Integer total = appointMentService.getTotal(phone, areaId, machineNo, userCode);
        return JsonResult.ok(appointMents, total);
    }

    /**
     * 根据用户id取消预约, 取消及删除有效预约，但是记录存在另外一个表
     *
     * @param userId   用户id
     * @param operaPsw 操作密钥
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.APPOINTMENT, methods = MethodEnum.APPOINTMENT_DEL)
    public JsonResult cancelAppointment(@NotNull Integer userId, String operaPsw) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(ServletUtils.getRequest()),
                    OperaPswEnum.OPERA_PSW_OPERATION_CONFIG.getModule(), operaPsw);

        appointMentService.cancal(userId);
        return JsonResult.ok();
    }
}
